package com.isolation.harassed.common.util;

import java.util.UUID;

import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class PlatformUtil {

	/**
	 * 获得当前设备号，且通常情况下是唯一的
	 * @param cxt android.content.Context
	 * @return the UUID
	 * @note 涉及：<p>{@link android.telephony.TelephonyManager#getDeviceId getDeviceId() }
	 * <p>{@link android.telephony.TelephonyManager#getSimSerialNumber getSimSerialNumber() }
	 * <p> {@link android.provider.Settings.Secure#ANDROID_ID ANDROID_ID }
	 * <p>Requires Permission:
     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
	 */
	public static final String getDeviceUUID(Context cxt) {

		if (cxt == null) {
			return "unknown";
		}
		final String deviceId, serialId, androidId;
		final TelephonyManager tm = (TelephonyManager) cxt
				.getSystemService(Context.TELEPHONY_SERVICE);
		deviceId = tm.getDeviceId();
		serialId = tm.getSimSerialNumber();
		androidId = Secure.getString(cxt.getContentResolver(),
				Secure.ANDROID_ID);
		long leastSigBits = ((long) deviceId.hashCode() << 32)
				| serialId.hashCode();
		UUID deviceUuid = new UUID(androidId.hashCode(), leastSigBits);
		String uuid = deviceUuid.toString();
		return uuid.substring(uuid.indexOf("-") + 1).replaceAll("-", "");
	}
}
